<?php
namespace Lib;
Vendor('phpQuery.phpQuery');
header("Content-Type: text/html; charset=UTF-8");
/* 文章抓取类 */
class WordsQuery
{

    public function GetWords($url)
    {

        $result = $this->do_curl($url);

        $result = str_replace("<!--headTrap<body></body><head></head><html></html>-->", "", $result);

        return phpQuery::newDocumentHTML($result);

    }


    protected function do_curl($url,$data='',$agent='')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT,10);
        if($data){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        if($agent){
            curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        }
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }

    public function unicode_decode($unistr, $encoding = 'utf-8', $prefix = '&#', $postfix = ';') {
        $arruni = explode($prefix, $unistr);
        $unistr = '';
        for ($i = 1, $len = count($arruni); $i < $len; $i++) {
            if (strlen($postfix) > 0) {
                $arruni[$i] = substr($arruni[$i], 0, strlen($arruni[$i]) - strlen($postfix));
            }
            $temp = intval($arruni[$i]);
            $unistr .= ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256);
        }
        return iconv('UCS-2', $encoding, $unistr);
    }

}
